// pages/select_identity/select_identity.js
const app = getApp();
const request = require('../../utils/request.js');
const url = require('../../utils/url.js');
const util = require('../../utils/util.js');
const cookie = require('../../utils/cookie.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    token: '',
    userinfo: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;

        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token: res.token
          })
          that.getUserInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  gouserindex() {
    wx.setStorageSync('lastModePage', 1)
    wx.navigateTo({
      url: '../user_end/index/index',
    })
  },
  checkUser: function () {
	  console.log(this.data.userinfo)
    if (this.data.userinfo.group_id == 2 && this.data.userinfo.checkinfo == 1) {
      wx.setStorageSync('lastModePage', 2)
      console.log('goindex');
      wx.requestSubscribeMessage({
        tmplIds: [
         'q9Js-vQrnKFfxtsIYMrYF6CQgNNsPITiZtcznzk8P-M',
         'WCOeBpOwto_yU4ZM-hUmQePvbfY_lxKyaFK5Yi7nzao',		   
         'SOnJPXow5f0XddOr1jQ-3wdd6V1SifOHJMdQtPbJNo0'
        ],
        success: (res) => {
          wx.navigateTo({
            url: '../server_end/index/index'
          })
        },
        fail: (res) => {
          wx.navigateTo({
            url: '../server_end/index/index'
          })
        },
      })

    } else if (this.data.userinfo.group_id == 2 && this.data.userinfo.checkinfo == 0) {
      console.log('goindex');
      wx.showToast({
        title: "请等待平台审核!",
        icon: "none"
      })
    } else if (this.data.userinfo.group_id == 2 && this.data.userinfo.checkinfo == 2) {
      wx.navigateTo({
        url: '../server_end/apply_server/apply_server'
      })
    } else {
      console.log('goapply');
      wx.navigateTo({
        url: '../server_end/apply_server/apply_server'
      })
    }
  },
  getUserInfo: function () {
    var that = this;
    let param = {
      scoure: 'wxapp',
      token: that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.getuserinfo, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if (res.data.code == 1) {
        that.setData({
          userinfo: res.data.data
        })
      }
    })
  }
})